<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>03 注册</title>
    <script src="./js/axios.min.js"></script>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <p>
            <label for="">用户名</label>
            <input type="text" v-model="username">
        </p>
        <p>
            <label for="">手机号</label>
            <input type="number" v-model="userphone">
        </p>
        <p>
            <label for="">密码&emsp;</label>
            <input type="text" v-model="userpwd">
        </p>
        <p>
            <button @click="register">注册</button>
        </p>
    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            username: "",
            userphone: "",
            userpwd: "",

        },
        methods: {
            async register() {

                // 新增之前判断用户名是否存在
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2345/userinfo",
                    params: {
                        username: this.username,
                        // userage:this.userage,
                    },
                });



                if (list.length) {
                    alert("用户名已经存在");
                } else {
                    let { data } = await axios({
                        method: "post",
                        url: "http://localhost:2345/userinfo",
                        data: {
                            username: this.username,
                            userphone:this.userphone,
                            userpwd:this.userpwd,
                        },
                    })

                    console.log(data)
                    window.location.href="./04 登录.html"
                }
            }

        }
    })

</script>

</html>